<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
      <el-form-item label="关键字搜索" prop="search">
        <el-input v-model="queryParams.search" placeholder="关键字搜索" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="装饰类型" prop="search">
        <el-select clearable v-model="queryParams.dressType"  style="width:100%">
          <el-option v-for="item in dressTypeList" :key="item.id" :value="item.id" :label="item.name">{{ item.name}}</el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
          range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['user:dress:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['user:dress:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['user:dress:remove']">删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['user:dress:export']">导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="装扮名称" align="center" prop="name" />
      <el-table-column label="装饰类型" align="center" prop="dressType">
        <template slot-scope="scope">
          <span>{{ scope.row.dressType == 1 ? '头像框' : scope.row.dressType == 2 ? '聊天气泡' : scope.row.dressType == 3 ?'音波' : '' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="图片" align="center" prop="dressImg">
        <template slot-scope="scope">
          <img :src="scope.row.dressImg" style="width: 80px;height: 60px;">
        </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{ scope.row.status == 1 ? '正常' : scope.row.status == 0 ? '关闭' : '' }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="装扮视频特效" align="center" prop="video" >
        <template slot-scope="scope">
          <video :src="scope.row.video" controls style="width: 120px; height: 120px" />
        </template>
      </el-table-column> -->
      <el-table-column label="等级" align="center" prop="grade">
        <template slot-scope="scope">
          <span>{{ scope.row.grade == 1 ? '普通' : scope.row.grade == 2 ? '中级' : scope.row.grade == 3 ? '高级' : ''
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['user:dress:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['user:dress:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改装扮中心对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>

        <el-form-item label="图片" prop="dressImg">
          <ImageUpload @input="uploadSuccess" :value="form.dressImg" />
        </el-form-item>

        <el-form-item label="Gif图" prop="dressGif">
          <ImageUpload @input="uploadGifSuccess" :value="form.dressGif" />
        </el-form-item>

        <el-form-item label="装扮类型" prop="dressType">
          <el-select clearable v-model="form.dressType" style="width:100%">
            <el-option v-for="item in dressTypeList" :key="item.id" :value="item.id" :label="item.name">{{ item.name}}</el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>

        <el-form-item label="装扮视频特效" prop="video">
          <FileUpload @input="uploadVideoSuccess" />
          <video :src="form.video" controls style="width: 160px; height: 160px" />
        </el-form-item>

        <el-form-item label="等级" prop="grade">
          <el-select v-model="form.grade">
            <el-option label="普通" :value="1"></el-option>
            <el-option label="中级" :value="2"></el-option>
            <el-option label="高级" :value="3"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDress, getDress, delDress, addDress, updateDress } from "@/api/dress/dress";

export default {
  name: "Dress",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 装扮中心表格数据
      dressTypeList: [{ id: 1, name: '头像框' },
      { id: 2, name: '聊天气泡' },
      { id: 3, name: '音波' },
      { id: 4, name: '进场特效' },
      { id: 5, name: '房间背景图' }],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        page: 1,
        size: 10,
        search: "",
        dressType: ''
      },
      // 表单参数
      form: {},
      dressList: [],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        dressType: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        dressImg: [
          { required: true, message: "图片不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "价格不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        grade: [
          { required: true, message: "等级1普通2中级3高级不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        isDel: [
          { required: true, message: "0未删除1已删除不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    uploadVideoSuccess(res) {
      this.form.video = res;
    },
    uploadSuccess(res) {
      this.form.dressImg = res;
    },
    uploadGifSuccess(res) {
      this.form.dressGif = res;
    },
    /** 查询装扮中心列表 */
    getList() {
      this.loading = true;
      listDress(this.queryParams).then(response => {
        this.dressList = response.data;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        dressType: null,
        dressImg: null,
        price: null,
        status: null,
        video: null,
        createTime: null,
        grade: null,
        updateTime: null,
        remark: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.dressType='';
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加装扮中心";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDress(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改装扮中心";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          addDress(this.form).then(response => {
            this.$modal.msgSuccess("操作成功");
            this.open = false;
            this.getList();
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除装扮中心编号为"' + ids + '"的数据项？').then(function () {
        return delDress(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/dress/export', {
        ...this.queryParams
      }, `dress_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
